<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="450px" :title="`${drawerProps.title}产品分类`">
    <el-form
      ref="ruleFormRef"
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.row"
      :hide-required-asterisk="drawerProps.isView"
    >
      <template v-if="drawerProps.row!.parentName || !drawerProps.row!.id">
        <el-form-item label="父级分类" prop="parentName">
          <el-select placeholder="选择父类" v-model="drawerProps.row!.parentName">
            <el-option v-for="ca in cas" :key="ca.id" :value="ca.name">
              {{ ca.name }}
            </el-option>
          </el-select>
        </el-form-item>
      </template>
      <el-form-item label="分类名字" prop="name">
        <el-input v-model="drawerProps.row!.name" placeholder="请填写分类名字" clearable></el-input>
      </el-form-item>
      <el-form-item label="分类图片" prop="picture">
        <UploadImg v-model:image-url="drawerProps.row!.picture" width="135px" height="135px" :file-size="3">
          <template #empty>
            <el-icon>
              <Picture />
            </el-icon>
            <span>请上传图片</span>
          </template>
          <template #tip> 图片大小不能超过 3M</template>
        </UploadImg>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false">取消</el-button>
      <el-button v-show="!drawerProps.isView" type="primary" @click="handleSubmit">确定</el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="ButtonDrawer">
import { ref, reactive } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import { Category } from "@/api/interface";
import UploadImg from "@/components/Upload/Img.vue";
import { Picture } from "@element-plus/icons-vue";
import { getCategoryList } from "@/api/modules/category";

const rules = reactive({
  name: [{ required: true, message: "请填写分类名字" }],
  parentName: [{ required: true, message: "请选择父级分类" }],
  picture: [{ required: true, message: "请选择图片" }]
});

interface DrawerProps {
  title: string;
  isView: boolean;
  row: Partial<Category.ResCategory>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  row: {}
});

// 接收父组件传过来的参数
const acceptParams = (params: DrawerProps) => {
  drawerProps.value = params;
  drawerVisible.value = true;
  getCas();
};

const cas = ref();
const getCas = async () => {
  const { data } = await getCategoryList({ id: null, name: null });
  cas.value = data;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      await drawerProps.value.api!(drawerProps.value.row);
      ElMessage.success({ message: `${drawerProps.value.title}分类信息成功！` });
      drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams
});
</script>
